define(['vue','iview','staff-table','staff-modal','staff-search','api'],function (Vue,iView,StaffTable,StaffModal,StaffSearch,api) {
    Vue.use(iView)
    window.bus = new Vue();
    var app = new Vue({
        el: '#app',
        components: {
            StaffModal: StaffModal,
            StaffTable: StaffTable,
            StaffSearch: StaffSearch
        },
        data: {
            dataList: [],
            currNo: 1,
            pageSize: 18,
            rowTotal: 0,
            secModel: '',
            postModel: '',
            limitModel: '',
            searchModel: '',
            secList: [],
            postList: [],
            limitList: []
        },
        watch: {
            secModel: function () {
                this.fetchDataList();
            },
            postModel: function () {
                this.fetchDataList();
            },
            limitModel: function () {
                this.fetchDataList();
            },
            searchModel: function () {
                this.fetchDataList();
            }
        },
        created: function () {
            this.fetchDataList();
            this.fetchOtherList();
        },
        methods: {
            onSocialSecurity: function (res) {
                var that = this
                res.forEach(function (item, number) {
                    that.dataList.forEach(function (t, index) {
                        if (item.CompanyId === t.CompanyId) that.dataList[index] = res[number]
                    })
                })
                this.dataList = this.dataList.slice()
            },
            onSaveStaff: function (data, status) {
                if (status !== 'edit') {
                    this.dataList.push(data);
                } else {
                    this.dataList.forEach(function (item, index, input) {
                        if (item.StaffId === data.StaffId) input[index] = data;
                    });
                    this.dataList = this.dataList.slice();
                }
            },
            handleChangePage: function (pageNo) {
                this.currNo = pageNo;
                this.fetchDataList();
            },
            fetchDataList: function () {
                var that = this;
                var params = {
                    companyId: localStorage.companyId,
                    departmentId: this.secModel[this.secModel.length - 1],
                    positionId: this.postModel,
                    permissionId: this.limitModel[this.limitModel.length - 1],
                    searchKey: this.searchModel,
                    pageSize: this.pageSize,
                    pageNo: this.currNo
                };

                api.fetchStaffList(params)
                    .done(function (res) {
                        if (res.IsSuccess) {
                            that.dataList = res.ResultObject;
                            that.rowTotal = res.rowTotal;
                        }
                    })
            },
            fetchOtherList: function () {
                var that = this;
                api.GetOrganizaitonNodes({companyId: localStorage.companyId})
                    .done(function (res) {
                        if (res.IsSuccess) {
                            that.secList = res.ResultObject;
                        }
                    });

                api.fetchPositionList({companyId: localStorage.companyId})
                    .done(function (res) {
                        if (res.IsSuccess) {
                            that.postList = res.ResultObject;
                        }
                    });

                api.GetAllPermissions()
                    .done(function (res) {
                        if (res.IsSuccess) {
                            that.limitList = res.ResultObject;
                        }
                    })
            }
        }
    })
})
